import { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
import { map, Observable, tap } from "rxjs";
import { AjaxResult } from "../class/ajaxResult";

/**
 * @Description: 响应拦截器
 * @author zhaojiuyi
 * @date 2023/4/4
 */
export class ResponseTransformInterceptor implements NestInterceptor{
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {

    return next.handle().pipe(
      map((data)=>{
        const response = context.switchToHttp().getResponse();
        response.header('Content-Type', 'application/json; charset=utf-8');
        return AjaxResult.success(data)
      })
    )
  }

}
